#include "TextureEffect.h"
#include "VertexLayouts.h"
namespace devii
{

TextureEffect::TextureEffect(Pipeline* const pipeline) : Effect(pipeline)
{
	constant_buffer = 0;
}

TextureEffect::~TextureEffect()
{
	if(constant_buffer)
		delete constant_buffer;
}


bool TextureEffect::init(VertexBuffer * const buffer)
{
	vertex_shader = pipeline->createVertexShader("shaders/Texture.vs","texture_vs", NULL, NULL, 0);
	geometry_shader = pipeline->createGeometryShader("shaders/Texture.gs", "texture_gs");
	pixel_shader = pipeline->createPixelShader("shaders/Texture.ps","texture_ps");
	constant_buffer = pipeline->createConstantBuffer();
	constant_buffer->init(cbsize, 0);
	return true;
}

void TextureEffect::reload()
{
}

void TextureEffect::release()
{
}

void TextureEffect::prepare(Texture* const texture, const matrix& world, const matrix& view_proj,
	const float3& position, const float3& size, const float3& rotation)
{
	D3D11_MAPPED_SUBRESOURCE resource;
	HRESULT hr = constant_buffer->map(0, D3D11_MAP_WRITE_DISCARD, &resource);

	// Fetch the pointer of the data in the constant_buffer
	cbTextureEffect* buf = (cbTextureEffect*)resource.pData;

	// Write new data to it
	buf->pos = position;
	buf->size = size;
	buf->view_proj = view_proj;
	buf->world = world;

	// Lock the buffer for writing
	constant_buffer->unmap(0);


}

void TextureEffect::setupPipeline()
{
	pipeline->setVertexShader(vertex_shader);
	pipeline->setGeometryShader(geometry_shader);
	pipeline->setPixelShader(pixel_shader);
}

}